﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

using static NewtonsoftUse.JsonUse;

namespace NewtonsoftUse
{
    /// <summary>
    /// OptIn
    /// 默认情况下,所有的成员不会被序列化,
    /// 类中的成员只有标有特性JsonProperty的才会被序列化,
    /// </summary>
    //[JsonObject(MemberSerialization.OptIn)]
    public class Product
    {
        //[JsonProperty]
        public int ShopID { get; set; }
        //[JsonIgnore]
        public int Price { get; set; }
        public int Count { get; set; }
        //[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string Name { get; set; }

        public Product ProductSon { get; set; }
    };

    /// <summary>
    /// json基础使用
    /// </summary>
    public class JsonUse
    {
        public static List<Product> JsonList()
        {
            List<Product> ProductList = new List<Product>
                                                {
                                                    new Product
                                                    {
                                                        ShopID = 1,
                                                        Price=10,
                                                        Count=4,
                                                        Name = "商品1",
                                                        ProductSon = new Product 
                                                        {
                                                           ShopID = 1,
                                                           Price=11,
                                                           Count=4,
                                                           Name = "商品1子商品",
                                                        }
                                                        
                                                    },
                                                    new Product
                                                    {
                                                        ShopID = 2,
                                                         Price=11,
                                                        Count=3,
                                                        Name = null
                                                    },
                                                    //new Product
                                                    //{
                                                    //    ShopID = 1,
                                                    //     Price=12,
                                                    //    Count=1,
                                                    //    Name = "商品三"
                                                    //},
                                                    //new Product
                                                    //{
                                                    //    ShopID = 2,
                                                    //     Price=17,
                                                    //    Count=10,
                                                    //    Name = "商品四"
                                                    //},
                                                    //new Product
                                                    //{
                                                    //    ShopID = 3,
                                                    //    Price=13,
                                                    //    Count=2,
                                                    //    Name = "商品五"
                                                    //}
                                                };
            return ProductList;
        }

        /// <summary>
        /// json忽略指定字段
        /// </summary>
        public static void JsonIgnore()
        {
            var productList = JsonList();
            JsonSerializerSettings settings = new JsonSerializerSettings();



            //场景一，指定显示字段 
            {

                settings.Formatting = Newtonsoft.Json.Formatting.Indented;
                settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //settings.MaxDepth = 10;
                settings.ContractResolver = new JsonPropertyContractResolver(new List<string> { "Price" });
                Console.WriteLine("场景一：" + JsonConvert.SerializeObject(productList, settings));
            }
            {

                //场景二，忽略空值字段，空值数据不参与序列化
                //settings.NullValueHandling = NullValueHandling.Ignore;
                //Console.WriteLine("场景二：" + JsonConvert.SerializeObject(productList, settings));
            }



        }





        public class JsonPropertyContractResolver : DefaultContractResolver
        {
            List<string> lstInclude;
            public JsonPropertyContractResolver(List<string> includeProperties)
            {
                lstInclude = includeProperties;
            }

            protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
            {
                // 获取指定属性的属性描述
                //var product = Activator.CreateInstance(type);
                //foreach (var item in type.GetProperties())
                //{
                //    JsonIgnoreNullAttribute fieldIfno = item.GetCustomAttribute<JsonIgnoreNullAttribute>();
                //    if (fieldIfno == null)
                //        continue;

                //    if (item.GetValue(product) == null)
                //    {
                //        lstInclude.Add(item.Name);
                //    }
                //}
                //
                return base.CreateProperties(type, memberSerialization).Where(p => !lstInclude.Contains(p.PropertyName)).ToList();
            }
        }


        [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
        public class JsonIgnoreNullAttribute : Attribute
        {
            public JsonIgnoreNullAttribute()
            {
            }
        }
    }
}
